﻿using System;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Share.Controls;
using ASPNETVN.PORTAL.Components.Share;
using System.Collections.Generic;

namespace ASPNETVN.PORTAL.Modules.CMS.Controls
{
    public partial class Detail : ModulePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            var postCommentControl = (PostComment)PostComment1;
            
            postCommentControl.IsRequireApproved = Convert.ToBoolean(this.Settings["Comment.RequireApproved"].ToString());
            postCommentControl.Posted += PostComment_Posted;

            var commentListControl = (CommentList)CommentList1;
            commentListControl.IsRequireApproved = Convert.ToBoolean(this.Settings["Comment.RequireApproved"].ToString());
        }

        protected void PostComment_Posted(object sender, EventArgs e)
        {
            var commentListControl = (CommentList)CommentList1;
            commentListControl.Reload();
            Components.Item.IncreaseTotalComments(this.ItemID);

            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hlPrint.NavigateUrl = "~/Modules/ASPNETVN.PORTAL.Modules.CMS/Pages/Print.aspx?itemid=" + this.ItemID;

                Components.Item.IncreaseTotalViews(this.ItemID);
                Components.Item item = Components.Item.Load(this.ItemID);
                if (item != null)
                {
                    // Build page link.
                    PageLink pagelink = (PageLink)this.ParentControl.FindControl("MainPageLink");

                    int categoryId = item.CategoryID;
                    Category category = Category.Load(categoryId);
                    if (category != null)
                    {
                        Category parentCategory = Category.Load(category.ParentID);
                        if (parentCategory != null)
                        {
                            pagelink.AddLink(parentCategory.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", parentCategory.ID)));

                            string url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", category.ID));
                            pagelink.AddLink(category.Title, url);

                            pagelink.AddLink(item.Title, "#");
                        }
                    }

                    lblTitle.Text = item.Title;
                    lblApprovedDate.Text = item.ApprovedDate.ToLongDateString() + " - " + item.Authors;
                    lblTotalViews.Text = item.TotalViews.ToString("N0");
                    lblTotalComments.Text = item.TotalComments.ToString("N0");
                    ltrRate.Text = ImageHelper.BuildRateImage(item.Rate, Request);
                    lblMemo.Text = Server.HtmlDecode(item.Body);

                    var commentListControl = (CommentList)CommentList1;
                    commentListControl.Title = "Trả lời: " + lblTitle.Text;

                    var postCommentControl = (PostComment)PostComment1;
                    postCommentControl.Title = "Trả lời: " + lblTitle.Text;

                    // Set title text.                    
                    this.SetTitleText(item.Title);
                }
            }
        }

        protected void dlRelationItem_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                Response.Redirect(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, Convert.ToInt32(e.CommandArgument), ModulePages.Detail));
            }
        }

        protected void btnSendMail_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                string from = string.Empty;
                if (PortalSecurity.CurrentUser.Identity.IsAuthenticated)
                {
                    from = PortalSecurity.CurrentUser.Identity.Email;
                }

                StringBuilder body = new StringBuilder();
                body.Append("Xin chao!<br><br>");
                body.Append("Toi tim thay thong tin nay can chia se voi ban. Toi nghi ban quan tam.<br><br>");

                string url = Request.Url.AbsoluteUri;

                body.AppendFormat("Xem chi tiet tai day: <a href='{0}'>{0}</a><br><br>", url);

                body.AppendFormat("- Tieu de: <strong>{0}</strong><br><br>", lblTitle.Text);
                body.AppendFormat("- Thong diep: {0}<br><br>", txtMessage.Text);

                body.Append("<br>Chuc ban vui ve!");

                string[] emails = txtEmails.Text.Split(';');
                foreach (string email in emails)
                {
                    MailHelper.SendMail(from, email, string.Empty, string.Empty, "Thong tin chia se", body.ToString(), MailPriority.Normal);
                }
                txtEmails.Text = txtMessage.Text = string.Empty;
            }
        }
    }
}